import { Component, OnInit, Input, ViewChild } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { NzModalRef, NzNotificationService } from 'ng-zorro-antd';

@Component({
  selector: 'quartz-msg-modal',
  templateUrl: './quartz-msg-modal.component.html',
})
export class QuartzMsgModalComponent implements OnInit {
  @Input()
  record: any = {};
  sysTypes = [];
  i: any;

  @ViewChild('sf', { static: false }) sf: SFComponent;
  schema: SFSchema = {
    properties: {},
  };

  ui: SFUISchema = {
    '*': {
      spanLabelFixed: 100,
      grid: { span: 24 },
    },
  };

  constructor(public http: _HttpClient, private modal: NzModalRef, private notice: NzNotificationService) {}

  ngOnInit() {
    if (this.record.jobId) {
      this.http.get(`/cf/job/sys-job/${this.record.jobId}`).subscribe(res => {
        this.i = res.data;
      });
    }

    this.http.get(`/cf/admin/web-msg/meta/systype/JOB_EXCEPTION`).subscribe(res => {
      this.sysTypes = res.data.map(r => {
        return { label: `${r.msgName}(${r.msgCode})`, value: r.msgCode };
      });
      this.buildSF();
    });
  }

  close() {
    this.modal.destroy();
  }

  save(val) {
    this.http.post(`/cf/job/sys-job/msgcode?jobId=${val.jobId}&msgCode=${val.msgCode}`).subscribe(res => {
      this.notice.success('成功', '成功设置消息类型');
      this.close();
    });
  }

  buildSF() {
    this.schema = {
      properties: {
        msgCode: {
          type: 'string',
          title: '通知类型',
          enum: this.sysTypes,
          description: '任务执行失败将发送指定类型的通知',
          ui: {
            widget: 'select',
            allowClear: true,
          },
        },
      },
    };
  }
}
